/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/CreateGame.fx $
******************************************************************************/

package de.hft_stuttgart.robofight.client.javafx;

import de.hft_stuttgart.robofight.client.java.JPlayer;
import de.hft_stuttgart.robofight.client.javafx.Constants;
import de.hft_stuttgart.robofight.client.javafx.util.*;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;
import javafx.scene.control.TextBox;
import com.sun.javafx.scene.control.caspian.TextBoxSkin;
import javafx.scene.Cursor;
import javafx.scene.control.Button;

import javafx.scene.input.MouseEvent;


/**
 * @author ralf.dauenhauer@gmx.de
 */

public class CreateGame extends CustomNode{

    package var getPlayer : function() : JPlayer;
    package var getNodeByName : function(nodeName : String) : Node;
    package var switchNode : function(className : String) : Void;

    public var allMaps : java.util.List = getPlayer().getAvailableMaps();
    public var numberOfPlayers  = ["2","3","4"];
    public var maxBots  = ["1","2","3","4","5"];
    public var maxLevel  = ["1","2","3","4"];
    public var buttonStatus = true;
   
    var gameNameField = TextBox {
        selectOnFocus: true
        layoutX:getNodeByName("createGameNameField").layoutBounds.minX + 7,
        layoutY:getNodeByName("createGameNameField").layoutBounds.minY + 7,
        font : Constants.FONT_TEXTFIELD,
        height : 24
        width: 225
        skin: TextBoxSkin {
            backgroundFill: Color.rgb(11, 17, 16)
            borderFill: Color.rgb(11, 17, 16)
            caretFill: Color.WHITE
            focusFill: Color.rgb(11, 17, 16)
            highlightFill: Color.rgb(11, 17, 16)
            promptTextFill: Color.rgb(11, 17, 16)
            selectedTextFill: Color.BLUE
            shadowFill: Color.rgb(11, 17, 16)
            textFill: Color.WHITE
            cornerRadius: 15
        }
    }


    var mapCombo = SwingComboBox{
        layoutX: getNodeByName("createGameMapField").layoutBounds.minX + 4;
        layoutY: getNodeByName("createGameMapField").layoutBounds.minY + 3;
        selectedIndex:0
        width: 240
        height: 32
        clip : Rectangle { width: 240, height: 32
                        arcHeight: 25
                        arcWidth: 25 }
        foreground:Color.BLACK
        opacity: 0.7
        font: Constants.FONT_TEXTFIELD
        items : for(map in allMaps){
            SwingComboBoxItem{
                text: map as String
            }
        }
    }

    var maxPlayerCombo = SwingComboBox{
        layoutX: getNodeByName("createGameMaxPlayersField").layoutBounds.minX + 4;
        layoutY: getNodeByName("createGameMaxPlayersField").layoutBounds.minY + 3;
        selectedIndex:0
        width: 240
        height: 32
        foreground:Color.BLACK
        opacity: 0.7
        clip : Rectangle { width: 240, height: 32
                        arcHeight: 25
                        arcWidth: 25 }
        font: Constants.FONT_TEXTFIELD
        items : for(players in numberOfPlayers){
            SwingComboBoxItem{
                text: players as String
            }
        }
    }

    var maxBotsCombo = SwingComboBox{
        layoutX: getNodeByName("createGameMaxBotsField").layoutBounds.minX + 4;
        layoutY: getNodeByName("createGameMaxBotsField").layoutBounds.minY + 3;
        selectedIndex:0
        width: 240
        height: 32
        foreground:Color.BLACK
        opacity: 0.7
        clip : Rectangle { width: 240, height: 32
                        arcHeight: 25
                        arcWidth: 25 }
        font: Constants.FONT_TEXTFIELD
        items : for(players in maxBots){
            SwingComboBoxItem{
                text: players as String
            }
        }
    }

    var maxLevelCombo = SwingComboBox{
        layoutX: getNodeByName("createGamePlayerLevelField").layoutBounds.minX + 4;
        layoutY: getNodeByName("createGamePlayerLevelField").layoutBounds.minY + 3;
        selectedIndex:0
        width: 240
        height: 32
        clip : Rectangle { width: 240, height: 32
                        arcHeight: 25
                        arcWidth: 25 }
        foreground:Color.BLACK
        opacity: 0.7
        font: Constants.FONT_TEXTFIELD
        items : for(players in maxLevel){
            SwingComboBoxItem{
                text: players as String
            }
        }
    }


    var group : Group = Group {
            content: [
            getNodeByName("background"),
            getNodeByName("logo_small"),
            Rectangle {
                var fxzLayout = getNodeByName("logout").layoutBounds;
                x : fxzLayout.minX;
                y : fxzLayout.minY;
                width : 80
                height : 15
                cursor : Cursor.HAND;
                onMouseClicked : function( e : MouseEvent) : Void {
                    getPlayer().logout();
                    switchNode("Login");
                }
                opacity:0
            },
            getNodeByName("createGameFrame"),
            getNodeByName("createGameName"),
            getNodeByName("createGameMap"),
            getNodeByName("createGameMaxPlayers"),
            getNodeByName("createGameMaxBots"),
            getNodeByName("createGamePlayerLevel"),
            getNodeByName("createGameNameField"), 
            getNodeByName("createGameMapField"),
            getNodeByName("createGameMaxPlayersField"),
            getNodeByName("createGameMaxBotsField"),
            getNodeByName("createGamePlayerLevelField"),
                getNodeByName("logout"),
            gameNameField,
            Button { // create a new Game
                    layoutX : getNodeByName("createGameSave").layoutBounds.minX
                    layoutY : getNodeByName("createGameSave").layoutBounds.minY
                    text: "createGame"
                    action: function():Void {
                        if(buttonStatus){
                            createGame();
                        } else{
                            java.lang.System.out.println("Allready in creating status");
                        }

                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
            },
            Button { //cancel
                    layoutX : getNodeByName("createGameCancel").layoutBounds.minX
                    layoutY : getNodeByName("createGameCancel").layoutBounds.minY
                    text: "cancel"
                    action: function():Void {
                        switchNode("Lobby");
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
            },
            mapCombo,
            maxPlayerCombo,
            maxBotsCombo,
            maxLevelCombo,
         ]
         };

    init {
        gameNameField.requestFocus();
    }

    override function create() : Node{
        return group;
    }


    /**
     * creates a new game with the parameters of the textfields
     */
    function createGame(){
        if(gameNameField.text == ""){
            insert Message {
                    msg: "Sorry, the text field for the game name can not be empty!"
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")

           } into group.content;
        } else {
        buttonStatus = false;
        if(getPlayer().createGame(
            gameNameField.text as String,
            mapCombo.getJComboBox().getSelectedItem().toString(),
            maxPlayerCombo.getJComboBox().getSelectedItem().toString(),
            maxBotsCombo.getJComboBox().getSelectedItem().toString(),
            maxLevelCombo.getJComboBox().getSelectedItem().toString(),)){
                switchNode("GameLobby");
        } else {
            insert Message {
                    msg: "Please check your input!"
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")
                    } into group.content;
        }
        }
    }
}
